/**
 * 2024/01/01 - 2024/12/31
 **/
package com.snail.camunda.engin.identity.manager;

import com.snail.camunda.engin.identity.dto.TenantInsertDTO;
import com.snail.camunda.engin.identity.dto.TenantModifyDTO;
import com.snail.camunda.engin.identity.dto.TenantQueryDTO;
import com.snail.camunda.engin.identity.holder.TenantHolder;
import com.snail.camunda.engin.identity.mapping.TenantMapping;
import com.snail.camunda.engin.identity.vo.TenantVO;
import com.snail.starter.commons.page.PageQO;
import com.snail.starter.commons.page.PageVO;
import org.camunda.bpm.engine.identity.Tenant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author: (zg)
 * @Date: 2024/12/16 15:13
 * @Version: 1.0.0
 */
@Component
public class TenantManager {

    @Resource
    TenantHolder tenantHolder;

    @Resource
    TenantMapping tenantMapping;

    public String add(TenantInsertDTO dto) {
        return tenantHolder.add(dto);
    }

    public boolean modify(String id, TenantModifyDTO dto) {
        return tenantHolder.modify(id, dto);
    }

    public boolean delete(String id) {
        return tenantHolder.deleteById(id);
    }

    public TenantVO queryById(String id) {
        return tenantMapping.entity2Vo(tenantHolder.queryById(id));
    }

    public List<TenantVO> list(TenantQueryDTO dto) {
        List<Tenant> list = tenantHolder.list(dto.getName());
        if(null == list || list.isEmpty()){
            return new ArrayList<>();
        }
        return list.stream().map(tenantMapping::entity2Vo).collect(Collectors.toList());
    }

    public PageVO<TenantVO> page(PageQO<String> page) {
        PageVO<Tenant> pageVO = tenantHolder.page(page);
        return PageVO.of(pageVO.getPageNum(), pageVO.getPageSize(), pageVO.getTotal(), pageVO.getData().stream().map(tenantMapping::entity2Vo).collect(Collectors.toList()));

    }
}